//
//  SecondViewController.m
//  SecondMeView
//
//  Created by lanou on 15/12/18.
//  Copyright © 2015年 吴昊. All rights reserved.
//

#import "SecondViewController.h"
#import "LORequestManger.h"
#import "Picture.h"
#import "CollectionViewCell.h"
#import "MJRefresh.h"
#import "BaseScrollView.h"
#import "Photos.h"
#import "PhotosTableViewCell.h"
#import "Classify.h"
#import "SecondTableViewCell.h"
#import "SecondDetailTableViewController.h"
#import "PictureDetailTableViewController.h"

static NSInteger i = 1;
static NSInteger selectTableFirstIndex = 0;//选择第一个的下标

@interface SecondViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong)NSMutableArray *pictureArray;//存放图片的数组
@property (nonatomic,strong)BaseScrollView *baseScrollView;
@property (nonatomic,strong)NSMutableArray *photosArray;//存放画册的数组

@property (nonatomic,strong)UIButton *allButton;//导航条上的button
@property (nonatomic,strong)NSMutableArray *classifyArray;//存放分类的数组
@property (nonatomic,strong)UITableView *SecondTableView;//分类里面的第二个tableView
@property (nonatomic,strong)UITableView *FirstTableView;//分类里面的第一个tableView
@property (nonatomic,strong)NSMutableArray *classifyArray2;//存放第二个tableView的值的数组

@property (nonatomic,strong)NSIndexPath *firstTableIndexPath;
@property (nonatomic,strong)NSMutableDictionary *classifyDic;
@property (nonatomic,strong)UIButton *pictureButton;
@property (nonatomic,strong)UIButton *photoButton;
@property (nonatomic,strong)UIView *orangeLineView;//button下面的橘黄色的下划线





@end

@implementation SecondViewController




- (void)viewDidAppear:(BOOL)animated{

   
    self.navigationController.navigationBar.translucent = NO;
}

- (void)viewWillAppear:(BOOL)animated{
    
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}


- (NSMutableArray *)pictureArray{
    if (_pictureArray == nil) {
        _pictureArray = [[NSMutableArray alloc]init];
    }
    return _pictureArray;
}

- (NSMutableArray *)photosArray{
    if (_photosArray == nil) {
        _photosArray = [[NSMutableArray alloc]init];
    }
    return _photosArray;
}

- (NSMutableArray *)classifyArray{
    if (_classifyArray == nil) {
        _classifyArray = [[NSMutableArray alloc]init];
        
    }
    return _classifyArray;
}

- (NSMutableArray *)classifyArray2{
    if (_classifyArray2 == nil) {
        _classifyArray2 = [[NSMutableArray alloc]init];
        
    }
    return _classifyArray2;
}



- (NSMutableDictionary *)classifyDic{
    if (_classifyDic == nil) {
        _classifyDic = [[NSMutableDictionary alloc]init];
    }
    return _classifyDic;
}


//图片页面的网络解析
- (void)jsonWithPicture{
    
    NSString *url = nil;
    if (self.classify == nil) {
        url = [NSString stringWithFormat:kSecondPitctureUrl,i];
        
    }else{
        url = [NSString stringWithFormat:kTheSecondClassify,i,self.classify.query];
        NSLog(@"%@",url);
        
    }
   
    [LORequestManger GET:url success:^(id response) {
        NSDictionary *dic = (NSDictionary *)response;
        NSArray *photos = dic[@"data"][@"idea_photos"][@"photos"];
        
        for (NSDictionary *dict in photos) {
            Picture *picture = [Picture shareWithDictionary:dict];
            [self.pictureArray addObject:picture];
        }
        [self.baseScrollView.pictureCollectionView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
}

//画册页面的网络解析
- (void)jsonWithPhoto{
    
    NSString *url = nil;
    if (self.classify == nil) {
        url = [NSString stringWithFormat:kSecondPhotoUrl,i];
    }else{
    
        url = [NSString stringWithFormat:kSecondClassifyPhoto,self.classify.query,i];
    }

    [LORequestManger GET:url success:^(id response) {
        NSDictionary *dic = (NSDictionary *)response;
        NSArray *array = dic[@"data"][@"idea_collections"][@"collections"];
      
        for (NSDictionary *dict in array) {
            Photos *photos =[Photos shareWithDictionary:dict];
            [self.photosArray addObject:photos];
        }
        
        [self.baseScrollView.photoTablleView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
}

//分类的网络解析
- (void)jsonWithClassify{
    [LORequestManger GET:kSecondClassify success:^(id response) {
        NSDictionary *dic = (NSDictionary *)response;
        NSArray *tags = dic[@"data"][@"idea_photos_tags2"][@"tags"];
        for (NSDictionary *dict in tags) {
            Classify *classify = [Classify shareWithDictionary:dict];
            NSArray *array = classify.tags;
            NSString *key = classify.class1;
            NSLog(@"=====%@",key);
            NSMutableArray *group = [[NSMutableArray alloc]init];
            for (NSDictionary *dict in array) {
                Classify *classify = [Classify shareWithDictionary:dict];
                [group addObject:classify];
            }
            [self.classifyArray2 addObject:key];
            [self.classifyDic setValue:group forKey:key];
            [self.classifyArray addObject:classify];
        }
        [self.FirstTableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
    
}


//下载通知的方法

- (void)huanChong{
    
    
    
    
    
    
    
}










- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self jsonWithPicture];
    [self jsonWithPhoto];
    [self jsonWithClassify];
    self.navigationController.navigationBar.alpha = 1;
    //UINavigationController上面的button
    self.allButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    self.allButton.frame = CGRectMake(kWidth / 2 - 50, 15, 100, 20);
    [self.allButton setTitle:@"全部" forState:(UIControlStateNormal)];
    self.allButton.tintColor = [UIColor blackColor];
    [self.allButton addTarget:self action:@selector(selectTableView) forControlEvents:(UIControlEventTouchUpInside)];
    [self.navigationController.navigationBar addSubview:self.allButton];
    self.allButton.tag = 1000;

    //添加一个view并添加两个button
    UIView *whiteView = [[UIView alloc]initWithFrame:(CGRectMake(0, 0, kWidth, 40))];
    whiteView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:whiteView];
    //图片的button
    self.pictureButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    self.pictureButton.frame = CGRectMake(kWidth / 3 - 50, 10, 100, 30);
    [self.pictureButton setTitle:@"图片" forState:(UIControlStateNormal)];
    [self.pictureButton setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
    [self.pictureButton addTarget:self action:@selector(turnToPhotoView) forControlEvents:(UIControlEventTouchUpInside)];
    [whiteView addSubview:self.pictureButton];
    //画册的button
    self.photoButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    self.photoButton.frame = CGRectMake(kWidth / 2, 10, 100, 30);
    [self.photoButton setTitle:@"画册" forState:(UIControlStateNormal)];
    [self.photoButton setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
    [self.photoButton addTarget:self action:@selector(turnToPictureView) forControlEvents:(UIControlEventTouchUpInside)];
    [whiteView addSubview:self.photoButton];
    
    self.orangeLineView = [[UIView alloc]initWithFrame:(CGRectMake(kWidth / 3 - 20, 38, 40, 2))];
    self.orangeLineView.backgroundColor = [UIColor orangeColor];
    [whiteView addSubview:self.orangeLineView];
    
    
    
    
    //----------------------------------------------------
    self.baseScrollView = [[BaseScrollView alloc]initWithFrame:(CGRectMake(0, 40, kWidth, kHeight))];
    self.baseScrollView.delegate = self;
    self.baseScrollView.pictureCollectionView.delegate =self;
    self.baseScrollView.pictureCollectionView.dataSource =self;
    self.baseScrollView.photoTablleView.delegate =self;
    self.baseScrollView.photoTablleView.dataSource =self;
    self.baseScrollView.backgroundColor =[UIColor yellowColor];
    [self.view addSubview:self.baseScrollView];
    [self.baseScrollView.pictureCollectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"cell1"];
    [self.baseScrollView.photoTablleView registerClass:[PhotosTableViewCell class] forCellReuseIdentifier:@"cell2"];
    
//下拉刷新(图片)
    __weak SecondViewController *secondVC = self;
    [self.baseScrollView.pictureCollectionView addFooterWithCallback:^{
        NSLog(@"下拉刷新");
        i +=1;
        [self jsonWithPicture];
        [secondVC.baseScrollView.pictureCollectionView footerEndRefreshing];
    }];
               
    //下拉刷新(画册)
   
    [self.baseScrollView.photoTablleView addFooterWithCallback:^{
        NSLog(@"下拉刷新");
        i +=1;
        [self jsonWithPhoto];
        [secondVC.baseScrollView.photoTablleView footerEndRefreshing];
    }];
   
    
   //分类里面的第一个tableView
    self.FirstTableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.FirstTableView.delegate = self;
    self.FirstTableView.dataSource = self;
    [self.FirstTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell3"];
    //分类里面的第二个tableView
    self.SecondTableView = [[UITableView alloc]initWithFrame:(CGRectMake(kWidth / 3,0, (kWidth / 3) *2, kHeight))];
    self.SecondTableView.delegate =self;
    self.SecondTableView.dataSource =self;
    [self.SecondTableView registerClass:[SecondTableViewCell class] forCellReuseIdentifier:@"cell4"];

    //对indexPath 的属性
    self.firstTableIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    
    
    //接受下载页面传过来的通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(huanChong) name:@"downloadOver" object:nil];
    
    
    
    
    // Do any additional setup after loading the view.
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    
    if (scrollView == self.baseScrollView) {
        CGFloat x = scrollView.contentOffset.x;
        if (x > 0) {
            NSLog(@"%.2f",x);
            [self.pictureButton setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
            [self.photoButton setTitleColor:[UIColor orangeColor] forState:(UIControlStateNormal)];
            self.orangeLineView.frame = CGRectMake( x / 3.4 + (kWidth / 3 - 20) , 38, 40, 2);
        }else{
            [self.photoButton setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
            self.orangeLineView.frame = CGRectMake( x / 3.4 + (kWidth / 3 - 20) , 38, 40, 2);
            [self.pictureButton setTitleColor:[UIColor orangeColor] forState:(UIControlStateNormal)];
        }
    }
}



//两个button的实现方法
- (void)turnToPhotoView{
 [self.photoButton setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
   [self.pictureButton setTitleColor:[UIColor orangeColor] forState:(UIControlStateNormal)];
    self.baseScrollView.contentOffset = CGPointMake(0, 0);
}

- (void)turnToPictureView{
    
    [self.pictureButton setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
     [self.photoButton setTitleColor:[UIColor orangeColor] forState:(UIControlStateNormal)];
    self.baseScrollView.contentOffset = CGPointMake(kWidth, 0);
}

//点击/UINavigationController上面的button的触发方法的实现
- (void)selectTableView{
    self.allButton.tag +=1;
    if (self.allButton.tag == 1001) {
        [self.baseScrollView removeFromSuperview];
        [self.view addSubview:self.FirstTableView];
        [self.view addSubview:self.SecondTableView];
    }
    if (self.allButton.tag == 1002){
        [self.FirstTableView removeFromSuperview];
        [self.SecondTableView removeFromSuperview];
        [self.view addSubview:self.baseScrollView];
        self.allButton.tag = 1000;
    }
}


#pragma mark -----collection的代理方法-----
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return [self.pictureArray count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell1" forIndexPath:indexPath];
//    cell.backgroundColor = [UIColor redColor];
   cell.picture = self.pictureArray[indexPath.item];
    return cell;
}
#pragma mark -----tableView的代理方法-----
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView == self.baseScrollView.photoTablleView) {
        return [self.photosArray count];
    }else if(tableView == self.FirstTableView){
       
        return [self.classifyArray count] + 1;
        
    }else{
    return [self.classifyDic[self.classifyArray2[selectTableFirstIndex]] count];
        return [_classifyDic[_classifyArray2[selectTableFirstIndex]] count];
    }
   
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (tableView == self.FirstTableView) {
        
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell3" ];
        cell.backgroundColor = [UIColor cyanColor];
        if (indexPath.row == 0) {
            cell.textLabel.text = @"全部";
        }else{
       
            Classify *classify = self.classifyArray[indexPath.row - 1];
            cell.textLabel.text = classify.class1;
        }
        
        
        
        return cell;
    }else if(tableView == self.baseScrollView.photoTablleView){
        PhotosTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell2" ];
        cell.photos = self.photosArray[indexPath.row];
        return cell;
        
    }else{
        
        SecondTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell4"];
        self.firstTableIndexPath = indexPath;
            cell.classify = self.classifyDic[self.classifyArray2[selectTableFirstIndex ]][self.firstTableIndexPath.row];
        
        
        
        return cell;
        
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == self.baseScrollView.photoTablleView) {
         return 140;
    }else if(tableView == self.FirstTableView) {
        return 60;
    }else{
        return 65;
    }
}
//collectionView的单元格的点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    PictureDetailTableViewController *pictureDTVC = [[PictureDetailTableViewController alloc]initWithStyle:(UITableViewStylePlain)];
  
    UINavigationController *nvc = [[UINavigationController alloc]initWithRootViewController:pictureDTVC];
    
    pictureDTVC.picture = self.pictureArray[indexPath.item];
    
      
    [self presentViewController:nvc animated:NO completion:nil];
    
    
}







//tableView点击单元格的方法的实现
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == self.FirstTableView) {
        
        if (indexPath.row == 0) {
            [self.FirstTableView removeFromSuperview];
            [self.SecondTableView removeFromSuperview];
            [self.allButton setTitle:@"全部" forState:(UIControlStateNormal)];
            self.classify = nil;
            [self.pictureArray removeAllObjects];
            [self.photosArray removeAllObjects];
            [self jsonWithPhoto];
            [self jsonWithPicture];
            [self.view addSubview:self.baseScrollView];
            self.allButton.tag = 1000;
        }else{
            selectTableFirstIndex = indexPath.row - 1;
            self.firstTableIndexPath = [NSIndexPath indexPathForRow:1 inSection:0];
            [self.SecondTableView reloadData];
        }
    }else if(tableView == self.SecondTableView){
        self.allButton.tag = 1000;
        [self.FirstTableView removeFromSuperview];
        [self.SecondTableView removeFromSuperview];
        
        [self.view addSubview:self.baseScrollView];
        
        self.classify =self.classifyDic[self.classifyArray2[selectTableFirstIndex]][indexPath.row];
        [self.allButton setTitle:self.classify.value forState:(UIControlStateNormal)];
        NSLog(@"%@",self.classify.query);
        [self.pictureArray removeAllObjects];
        [self.photosArray removeAllObjects];
        [self jsonWithPicture];
        [self jsonWithPhoto];
        [self.baseScrollView.photoTablleView reloadData];
        [self.baseScrollView.pictureCollectionView reloadData ];
    }else{
        
        SecondDetailTableViewController *secondDetailVC = [[SecondDetailTableViewController alloc]init];
       
        secondDetailVC.photos = self.photosArray[indexPath.row];
        NSLog(@"%@",secondDetailVC.photos.col_id);
        
        [self.navigationController pushViewController:secondDetailVC animated:YES];
        
    }
}









- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
